#ifdef NHNS_WINDOWS
#include <windows.h>
#include <stdio.h>
#endif
#ifdef NHNS_UNIX
#include <csignal>
#endif
#include "Server.hpp"

//Global access for exit handlers.
Server* _server;

#ifdef NHNS_WINDOWS
/**
 * Capture console shutdown and try to stop server nicely.
 */
BOOL CtrlHandler(DWORD fdwCtrlType){
    if(fdwCtrlType == CTRL_C_EVENT || fdwCtrlType == CTRL_BREAK_EVENT){
        //We can stop the server nicely since we will keep control after
        //this function returns.
        _server->stop();
    }else{
        //Server will not regain control after this function so
        //abruptly shutdown.
        _server->shutdown();
    }
    
    return TRUE;
}
#endif
#ifdef NHNS_UNIX

/** 
 * Capture unix terminating signals. Try to stop server nicely.
 */
void signalHandler(int sig){
    _server->stop();
}

#endif

int main(void){
#ifdef NHNS_WINDOWS
    SetConsoleCtrlHandler((PHANDLER_ROUTINE)CtrlHandler, TRUE);
#endif
#ifdef NHNS_UNIX
    //Capture terminating signals
    signal(SIGINT, signalHandler);
    signal(SIGTERM, signalHandler);
#endif
    
    Server server;
    _server = &server;
    FileLogger logger;
	int retVal = server.start();
    return retVal;
}